package study.itheima.Test21;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class TestDemo21 {
    public static void main(String[] args) throws IOException {
        /*
        键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩)。
        要求按照成绩总分从高到低写入文本文件，最后在从文件中把读取数据显示在控制台
         */
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
//            (自定义总分相同时,语文成绩从低到高排(从题中结果推导出来);语文成绩相同时,数学成绩从低到高排;都相同时比较姓名,如果以上都一样,认为两个人相同)
            public int compare(Student o1, Student o2) {
                int num1 = o2.sum() - o1.sum();
                int num2 = num1 == 0?o1.getYwScore()-o2.getYwScore():num1;
                int num3 = num2 == 0?o1.getSxScore()-o2.getSxScore():num2;
                int num4 = num3 == 0?o1.getName().compareTo(o2.getName()):num3;
                return num4;
            }
        });
        Scanner sc = new Scanner(System.in);
        //要求录入三个,循环三次
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入第" + i + "个学生成绩");
            System.out.println("姓名:");
            String name = sc.next();
            System.out.println("语文成绩:");
            int yuwen = sc.nextInt();
            System.out.println("数学成绩:");
            int shuxue = sc.nextInt();
            System.out.println("英语成绩:");
            int yingyu = sc.nextInt();
            Student s = new Student(name,yuwen,shuxue,yingyu);
            ts.add(s);
        }
        //写入文件,传学生对象进去
        BufferedWriter bw = new BufferedWriter(new FileWriter("secondtest\\score.txt"));
        for (Student t : ts) {
            bw.write(t.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
        //从文件中把读取数据显示在控制台
        BufferedReader br = new BufferedReader(new FileReader("secondtest\\score.txt"));
        String str;
        System.out.println("姓名,语文成绩,数学成绩,英语成绩");
        while ((str = br.readLine()) != null){
            System.out.println(str);
        }
        br.close();
    }
}
